class_name Enemy_Impl
extends CharacterBody2D

@export var health := 10
@export var type := "Default"

var cur_health: int

var attack_obj: CharacterBody2D

signal on_die(enemy: Enemy_Impl)
signal on_hurt(enemy: Enemy_Impl, hurt_dir: Vector2)
signal on_attack(enemy: Enemy_Impl)

func _ready() -> void:
	reset()
	pass

func _physics_process(delta: float) -> void:
	var player: Player = RoomManager.get_room_player(self)
	if player != attack_obj:
		set_attack_obj(player)

func reset() -> void:
	cur_health = health
	velocity = Vector2.ZERO
	for cb in on_die.get_connections():
		on_die.disconnect(cb.callable)
	for cb in on_hurt.get_connections():
		on_hurt.disconnect(cb.callable)
	for cb in on_attack.get_connections():
		on_attack.disconnect(cb.callable)

	on_die.connect(_on_die)
	on_hurt.connect(_on_hurt)
	on_attack.connect(_on_attack)

func _on_die(_enemy: Enemy_Impl) -> void:
	pass

func _on_hurt(_enemy: Enemy_Impl, _hurt_dir: Vector2) -> void:
	pass

func _on_attack(_enemy: Enemy_Impl) -> void:
	pass

func set_attack_obj(character: CharacterBody2D) -> void:
	attack_obj = character

func release() -> void:
	reset()
	EnemyManager.release_enemy(type, self)

func die() -> void:
	cur_health = 0
	on_die.emit(self)
	release()

func hurt(hurt_dir: Vector2) -> void:
	if cur_health > 0:
		cur_health -= 1
		on_hurt.emit(self, hurt_dir)
		if cur_health <= 0:
			die()

func attack() -> void:
	on_attack.emit(self)
	pass
